#!/usr/bin/env python

import gtk
import gettext
import constants
from mxit import handles
from blend import const

gettext.install('blendIT','locale');

class selectContactDialog(gtk.Dialog):

   checks = [];

   def __init__(self,blend,title=None,message=None,choice=1):
      window = blend.gui.buddyWindow;
      if title == None:
         title = _("Select Contacts");
      gtk.Dialog.__init__(self,title, window,
                          gtk.DIALOG_DESTROY_WITH_PARENT,
                          (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
                          gtk.STOCK_OK, gtk.RESPONSE_ACCEPT));

      self.blend = blend;
      self.set_icon_from_file(constants.IMG_Invite);
     
      self.choice = choice;
      self.drawDialog();
      if message != None:
         msgLabel = gtk.Label(message);
         msgLabel.show();
         self.vbox.pack_start(msgLabel);

      def sig(widget): widget.destroy();
      self.connect('close',sig);    

   def getSelectedContacts(self):
      if choice == 0:
         for i in range(0,len(self.checks)):
            if self.checks[i][0].get_active():
               return self.checks[i][1];
      else:
         buddies = [];
         for i in range(0,len(self.checks)):
            if self.checks[i][0].get_active():
               buddies.append(self.checks[i][1]);
      
      return buddies;

   def drawDialog(self):

      table = gtk.Table(rows=1,columns=2);
    
      # Contacts
      invLabel = gtk.Label(_("Invitations:"));
      invLabel.show();
      table.attach(invLabel,0,1,0,1,gtk.FILL);

      self.checks = [];
      box = gtk.VBox();
      box.show();
      
      # Scrolled Window
      buddies = self.blend.conn.roster.all_online_buddies();
      for i in range(0,len(buddies)):
         if buddies[i].isService() == False or buddies[i].isRoom() == False:
            if self.choice == 0:
               # add as option
               if i > 0:
                  d = self.checks[0][0];
               else:
                  d = None;
               check = gtk.RadioButton(d,str(buddies[i].name));
            else:
               # Add as a check
               check = gtk.CheckButton(str(buddies[i].name));
            
            check.show();
            self.checks.append((check,buddies[i]));
            box.pack_start(check,False,False);

      scrolledwindow = gtk.ScrolledWindow();
      scrolledwindow.add_with_viewport(box);
      scrolledwindow.set_policy(gtk.POLICY_AUTOMATIC,gtk.POLICY_AUTOMATIC);
      scrolledwindow.show();
      table.attach(scrolledwindow,1,2,0,1,gtk.EXPAND|gtk.FILL);
      
      
      table.show();
      table.set_row_spacings(4);
      table.set_col_spacings(4);
      self.get_content_area().add(table);
